package com.wuba.rxjavademo.lyz.day0416;

import java.util.concurrent.TimeUnit;

import rx.Observable;

/**
 * Created by liyaxing on 2016/4/20.
 */
public class Demo07 {

    public static void main(String[] ss){

        Demo07 demo07 = new Demo07() ;
//        Observable<Boolean> observable = demo07.allObservable() ;
//        observable.subscribe(aBoolean -> print("result : " + aBoolean)) ;
        Observable<Integer> observable = demo07.takeWhileObservable() ;
        observable.subscribe(obj -> print("result : " + obj)) ;

        try {
            Thread.sleep(50000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    private Observable<Integer> takeWhileObservable(){
        return Observable.just(1,2,3,4,5,6,7,9).takeWhile(integer -> integer < 3) ;
    }

    private Observable<Long> takeUntilObservable(){
        return Observable.interval(1,TimeUnit.SECONDS).takeUntil(Observable.timer(5,TimeUnit.SECONDS)) ;
    }

    private Observable<Integer> skipWhileObservable(){
        return Observable.just(1,2,3,4,5,6,7).skipWhile(integer -> integer < 3) ;
    }

    private Observable<Long> skipUntilObservable(){
        return Observable.interval(1,TimeUnit.SECONDS).skipUntil(Observable.timer(5, TimeUnit.SECONDS)) ;
    }

    private Observable<Boolean> sequenceEqualObservable(){
        return Observable.sequenceEqual(Observable.just(1,2,3,4),Observable.just(1,2,3,4)) ;
    }

    private Observable<Integer> defaultIsEmptyObservable(){
        return Observable.<Integer>create(subscriber -> subscriber.onCompleted()).defaultIfEmpty(999) ;
    }

    private Observable<Boolean> isEmptyObservable(){

        return Observable.create(subscriber -> subscriber.onCompleted()).isEmpty() ;

//        return Observable.just(1).isEmpty() ;
    }

    private Observable<Boolean> containsObservable(){
        return Observable.just(1,2,3,4).contains(4) ;
    }

    private Observable<Integer> ambObservable(){
        Observable<Integer> delay1 = Observable.just(1,2,3).delay(1000, TimeUnit.MILLISECONDS) ;
        Observable<Integer> delay2 = Observable.just(4,5,6).delay(2000, TimeUnit.MILLISECONDS) ;
        Observable<Integer> delay3 = Observable.just(7,8,9).delay(3000, TimeUnit.MILLISECONDS) ;
        return Observable.amb(delay1,delay2,delay3) ;
    }

    private Observable<Boolean> allObservable(){
        return Observable.just(1,2,3,4,5,6).all(integer -> integer < 7) ;
    }


    private static void print(Object obj){
        System.out.println(obj) ;
    }

}
